﻿using MiniFox.Components.Configurations;
using MiniFox.Configurations;
using System;
using System.Collections.Specialized;

namespace MiniFox.Components
{
    /// <summary>
    /// 
    /// </summary>
    internal class DefaultComponentContainer : ComponentContainer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public DefaultComponentContainer(IComponentContainerConfiguration configuration) : base(configuration)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contractType"></param>
        /// <returns></returns>
        public override bool IsRegistered(Type contractType)
        {
            string contractName = contractType.FullName;
            bool isRegistered = ComponentBuilders.Contains(contractType);
            if (!isRegistered)
            {
                foreach (IComponentConfig setting in this.Configuration.ComponentSettings)
                {
                    Type cType = Type.GetType(setting.ContractType);
                    if (cType != null && cType.FullName == contractName)
                    {
                        isRegistered = true;
                        break;
                    }
                }
            }
            return isRegistered;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contractType"></param>
        /// <param name="componentName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected override object ResovleComponent(Type contractType, string componentName, params object[] args)
        {
            RegisterComponent(contractType);

            return ComponentBuilders[contractType].ResolveComponentByName(componentName, args);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contractType"></param>
        /// <returns></returns>
        protected override IComponentConfig GetComponentConfig(Type contractType)
        {
            IComponentConfig config = null;
            string contractName = contractType.FullName;
            foreach (IComponentConfig setting in this.Configuration.ComponentSettings)
            {
                Type cType = Type.GetType(setting.ContractType);
                if (cType != null && cType.FullName == contractName)
                {
                    config = setting;
                    break;
                }
            }
            if (config == null)
                config = new ComponentConfig
                {
                    ContractType = contractType.AssemblyQualifiedName,
                };
            return config;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentConfig"></param>
        /// <returns></returns>
        protected override ComponentBuilder CreateComponentBuilder(IComponentConfig componentConfig)
        {
            ComponentBuilder builder = null;
            switch (componentConfig.LifeStyle)
            {
                case ComponentLifeStyleType.Singleton:
                    builder = new SingletonComponentBuilder(componentConfig);
                    break;
                case ComponentLifeStyleType.Transient:
                    builder = new DefaultComponentBuilder(componentConfig);
                    break;
            }
            return builder;
        }
    }
}
